package com.sumer.thread;

import java.util.concurrent.*;

/**
 * 创建线程方式3
 * 1.实现Callable接口
 * 2.重写call方法
 * 3.创建执行服务
 * 4.提交执行
 * 5.获取结果
 * 6.关闭服务
 * 好处：
 * ① 可以定义返回值
 * ② 可以抛出异常
 */
public class ThreadCallableTest implements Callable<Boolean> {
    @Override
    public Boolean call() throws Exception {
        System.out.println("执行call方法");
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ThreadCallableTest t1 = new ThreadCallableTest();
        ThreadCallableTest t2 = new ThreadCallableTest();
        ThreadCallableTest t3 = new ThreadCallableTest();

        // 创建执行服务
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        // 提交执行
        Future<Boolean> submit1 = executorService.submit(t1);
        Future<Boolean> submit2 = executorService.submit(t2);
        Future<Boolean> submit3 = executorService.submit(t3);
        // 获取结果
        System.out.println(submit1.get());
        System.out.println(submit2.get());
        System.out.println(submit3.get());
        // 关闭服务
        executorService.shutdownNow();
    }
}
